//
//  HDAreaChoiceView.m
//  WujieLive3.0_beta
//
//  Created by haidong on 2017/12/20.
//  Copyright © 2017年 loghdst. All rights reserved.
//

#import "HDAreaChoiceView.h"
#import "HDAreaShowView.h"
#import "NSDictionary+Help.h"
#import "HDRegionEntity.h"

@interface HDAreaChoiceView ()
<
BaseViewDelegate,
UITableViewDelegate,
UITableViewDataSource
>
{
    
}

@property (strong, nonatomic)HDRegionEntity *oneLevelEntity;
@property (strong, nonatomic)HDRegionEntity *twoLevelEntity;
@property (strong, nonatomic)HDRegionEntity *threeLevelEntity;

@property (strong, nonatomic)HDAreaShowView *areaShowView;
@property (strong, nonatomic)UITableView *tableView;

@property (strong, nonatomic)NSMutableArray *regionArray;
@property (strong, nonatomic)NSMutableArray *oneLevelRegionArray;
@property (strong, nonatomic)NSMutableArray *twoLevelRegionArray;
@property (strong, nonatomic)NSMutableArray *threeLevelRegionArray;

@property (strong, nonatomic)NSMutableArray *currentRegionArray;
@property (assign, nonatomic)NSInteger currentIndex;

@end

@implementation HDAreaChoiceView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.areaShowView];
        [self addSubview:self.tableView];
        self.currentIndex = 0;
        [self getRegionData];
    }
    return self;
}

- (void)getRegionData {
    self.regionArray = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"regionList.plist" ofType:nil]];
    self.oneLevelRegionArray = [self.regionArray mutableCopy];

    [self.currentRegionArray removeAllObjects];
    
    for (NSDictionary *dic in self.oneLevelRegionArray) {
        HDRegionEntity *entity = [HDRegionEntity new];
        entity.region_id = [dic objectForKey:@"region_id"];
        entity.region_name = [dic objectForKey:@"region_name"];
        [self.currentRegionArray addObject:entity];
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.currentRegionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"areaChoiceCellId"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.textColor = ColorFromRGB(0x333333);
    cell.textLabel.font = FontSize(14.f);
    cell.textLabel.text = [self.currentRegionArray[indexPath.row] region_name];
    switch (self.currentIndex) {
        case 0:
            if ([self.oneLevelEntity.region_id isEqualToString:[(HDRegionEntity *)self.currentRegionArray[indexPath.row] region_id]]) {
                cell.textLabel.textColor = ColorFromRGB(0xF03D37);
            }
            break;
        case 1:
            if ([self.twoLevelEntity.region_id isEqualToString:[(HDRegionEntity *)self.currentRegionArray[indexPath.row] region_id]]) {
                cell.textLabel.textColor = ColorFromRGB(0xF03D37);
            }
            break;
        case 2:
            if ([self.threeLevelEntity.region_id isEqualToString:[(HDRegionEntity *)self.currentRegionArray[indexPath.row] region_id]]) {
                cell.textLabel.textColor = ColorFromRGB(0xF03D37);
            }
            break;
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *regionStr = [self.currentRegionArray[indexPath.row] region_name];
    NSString *regionIdStr = [(HDRegionEntity *)self.currentRegionArray[indexPath.row] region_id];
    
    switch (self.currentIndex) {
        case 0:
        {
            self.oneLevelEntity.region_name = regionStr;
            self.oneLevelEntity.region_id = regionIdStr;
            
            self.twoLevelEntity = nil;
            [self.twoLevelRegionArray removeAllObjects];
            self.threeLevelEntity = nil;
            [self.threeLevelRegionArray removeAllObjects];
            
            NSArray *subRegionArray = [NSArray arrayWithArray:[self.regionArray[indexPath.row] arrForKey:@"son"]];
            self.twoLevelRegionArray = [subRegionArray mutableCopy];
            
            if (subRegionArray.count > 0) {
                [self.currentRegionArray removeAllObjects];
                for (NSDictionary *dic in self.twoLevelRegionArray) {
                    HDRegionEntity *entity = [HDRegionEntity new];
                    entity.region_id = [dic objectForKey:@"region_id"];
                    entity.region_name = [dic objectForKey:@"region_name"];
                    [self.currentRegionArray addObject:entity];
                }
            }
            
            [self.areaShowView showMessage:regionStr atIndex:self.currentIndex hasMore:self.twoLevelRegionArray.count > 0 ? YES : NO];
            self.currentIndex += self.twoLevelRegionArray.count > 0 ? 1 : 0;
        }
            break;
        case 1:
        {
            self.twoLevelEntity.region_name = regionStr;
            self.twoLevelEntity.region_id = regionIdStr;
            
            self.threeLevelEntity = nil;
            [self.threeLevelRegionArray removeAllObjects];
            
            NSArray *subRegionArray = [NSArray arrayWithArray:[self.twoLevelRegionArray[indexPath.row] arrForKey:@"son"]];
            self.threeLevelRegionArray = [subRegionArray mutableCopy];

            if (subRegionArray.count > 0) {
                [self.currentRegionArray removeAllObjects];
                for (NSDictionary *dic in subRegionArray) {
                    HDRegionEntity *entity = [HDRegionEntity new];
                    entity.region_id = [dic objectForKey:@"region_id"];
                    entity.region_name = [dic objectForKey:@"region_name"];
                    [self.currentRegionArray addObject:entity];
                }
            }

            [self.areaShowView showMessage:regionStr atIndex:self.currentIndex hasMore:self.threeLevelRegionArray.count > 0 ? YES : NO];
            self.currentIndex += self.threeLevelRegionArray.count > 0 ? 1 : 0;
        }
            break;
        case 2:
        {
            self.threeLevelEntity.region_name = regionStr;
            self.threeLevelEntity.region_id = regionIdStr;
            [self.areaShowView showMessage:regionStr atIndex:self.currentIndex hasMore:NO];
        }
            break;
        default:
            break;
    }
    [self.tableView reloadData];

}

- (void)viewActionWithView:(UIView *)view tag:(NSInteger)tag data:(id)data {
    if ([view isKindOfClass:[HDAreaShowView class]]) {
        self.currentIndex = tag;
        switch (tag) {
            case 0:
                [self.currentRegionArray removeAllObjects];
                for (NSDictionary *dic in self.oneLevelRegionArray) {
                    HDRegionEntity *entity = [HDRegionEntity new];
                    entity.region_id = [dic objectForKey:@"region_id"];
                    entity.region_name = [dic objectForKey:@"region_name"];
                    [self.currentRegionArray addObject:entity];
                }
                [self.tableView reloadData];
                break;
            case 1:
                [self.currentRegionArray removeAllObjects];
                for (NSDictionary *dic in self.twoLevelRegionArray) {
                    HDRegionEntity *entity = [HDRegionEntity new];
                    entity.region_id = [dic objectForKey:@"region_id"];
                    entity.region_name = [dic objectForKey:@"region_name"];
                    [self.currentRegionArray addObject:entity];
                }
                [self.tableView reloadData];
                break;
            case 2:
                [self.currentRegionArray removeAllObjects];
                for (NSDictionary *dic in self.threeLevelRegionArray) {
                    HDRegionEntity *entity = [HDRegionEntity new];
                    entity.region_id = [dic objectForKey:@"region_id"];
                    entity.region_name = [dic objectForKey:@"region_name"];
                    [self.currentRegionArray addObject:entity];
                }
                [self.tableView reloadData];
                break;
            case 3:
            {
                // 确定选择
                if (self.delegate && [self.delegate respondsToSelector:@selector(viewActionWithView:tag:data:)]) {
                    NSString *regionIdStr = @"";
                    if (self.oneLevelEntity.region_id && ![self.oneLevelEntity.region_id isEqualToString:@""]) {
                        regionIdStr = self.oneLevelEntity.region_id;
                    }
                    if (self.twoLevelEntity.region_id && ![self.twoLevelEntity.region_id isEqualToString:@""]) {
                        regionIdStr = self.twoLevelEntity.region_id;
                    }
                    if (self.threeLevelEntity.region_id && ![self.threeLevelEntity.region_id isEqualToString:@""]) {
                        regionIdStr = self.threeLevelEntity.region_id;
                    }
                    NSString *regionNameStr = [NSString stringWithFormat:@"%@%@%@", self.oneLevelEntity.region_name, self.twoLevelEntity.region_name, self.threeLevelEntity.region_name ? self.threeLevelEntity.region_name : @""];
                    [self.delegate viewActionWithView:self tag:0 data:@{@"region_id":regionIdStr,
                                                                        @"region_name":regionNameStr
                                                                        }];
                }
            }
                break;
            default:
                break;
        }
        [self.tableView reloadData];
    }
}

- (HDAreaShowView *)areaShowView {
    if (!_areaShowView) {
        _areaShowView = [[HDAreaShowView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50.f)];
        _areaShowView.delegate = self;
    }
    return _areaShowView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50.f, self.frame.size.width, self.frame.size.height - 50.f) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"areaChoiceCellId"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (HDRegionEntity *)oneLevelEntity {
    if (!_oneLevelEntity) {
        _oneLevelEntity = [HDRegionEntity new];
    }
    return _oneLevelEntity;
}

- (HDRegionEntity *)twoLevelEntity {
    if (!_twoLevelEntity) {
        _twoLevelEntity = [HDRegionEntity new];
    }
    return _twoLevelEntity;
}

- (HDRegionEntity *)threeLevelEntity {
    if (!_threeLevelEntity) {
        _threeLevelEntity = [HDRegionEntity new];
    }
    return _threeLevelEntity;
}

- (NSMutableArray *)regionArray {
    if (!_regionArray) {
        _regionArray = [NSMutableArray array];
    }
    return _regionArray;
}

- (NSMutableArray *)oneLevelRegionArray {
    if (!_oneLevelRegionArray) {
        _oneLevelRegionArray = [NSMutableArray array];
    }
    return _oneLevelRegionArray;
}

- (NSMutableArray *)twoLevelRegionArray {
    if (!_twoLevelRegionArray) {
        _twoLevelRegionArray = [NSMutableArray array];
    }
    return _twoLevelRegionArray;
}

- (NSMutableArray *)threeLevelRegionArray {
    if (!_threeLevelRegionArray) {
        _threeLevelRegionArray = [NSMutableArray array];
    }
    return _threeLevelRegionArray;
}

- (NSMutableArray *)currentRegionArray {
    if (!_currentRegionArray) {
        _currentRegionArray = [NSMutableArray array];
    }
    return _currentRegionArray;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
